
import org.junit.*;
import java.util.*;
import play.test.*;
import models.*;

/**
 *
 * @author Dina
 */
public class Dina extends UnitTest {

    @Test
    public void HidingUnhidingTopicInOrg() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Organization1");
        Topic t = Topic.create(dev, org);
        assertEquals(1, Topic.count());
        assertEquals(1, Topic.count("hidden =?", false));
        t.hideTopic();
        assertEquals(1, Topic.count("hidden =?", true));
        t.unhideTopic();
        assertEquals(1, Topic.count("hidden =?", false));
        t.delete();
        assertEquals(0, Topic.count());
    }

    @Test
    public void HidingUnhidingTopicInEntAndSubEnt() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Organization1");
        Entities entity = Entities.create(org);
        Entities subEntity = Entities.create(entity);
        Topic topic1 = Topic.create(dev, entity);
        Topic topic2 = Topic.create(dev, subEntity);
        assertEquals(2, Topic.count());
        assertEquals(2, Topic.count("hidden =?", false));
        topic1.hideTopic();
        assertEquals(1, Topic.count("hidden =?", true));
        assertEquals(1, Topic.count("hidden =?", false));
        topic2.hideTopic();
        assertEquals(2, Topic.count("hidden =?", true));
        assertEquals(0, Topic.count("hidden =?", false));
        topic1.unhideTopic();
        assertEquals(1, Topic.count("hidden =?", true));
        assertEquals(1, Topic.count("hidden =?", false));
        topic2.unhideTopic();
        assertEquals(0, Topic.count("hidden =?", true));
        assertEquals(2, Topic.count("hidden =?", false));
        topic1.delete();
        assertEquals(1, Topic.count());
        topic2.delete();
        assertEquals(0, Topic.count());
    }

    @Test
    public void VoteAndUnvoteIdeas() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        IdeaDeveloper dev2 = new IdeaDeveloper("Dina", "Dina", "Dina",
                "Dina", "1/1/2000", "Dina");
        dev2.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Org");
        Entities ent = Entities.create(org);
        Entities ent2 = Entities.create(ent);
        Topic t = Topic.create(dev, ent2);
        Idea it = Idea.create(dev, t);
        Idea io = Idea.create(dev, org);
        Idea ie = Idea.create(dev, ent);
        assertNotNull(io.ideaOrg);
        assertNotNull(ie.ideaEnt);
        assertNotNull(it.ideaTopic);
        org.members.add(dev2);
        org.save();
        IdeaDeveloper dev3 = (IdeaDeveloper) IdeaDeveloper.find("username =?", dev2.username).first();
        dev3.joinedOrg.add(org);
        dev3.save();
        assertEquals(1, dev2.joinedOrg.size());
        assertEquals(1, org.members.size());
        assertEquals(0, it.noOfVotes);
        assertEquals(0, io.noOfVotes);
        assertEquals(0, ie.noOfVotes);
        it.vote(dev2.id);
        io.vote(dev2.id);
        ie.vote(dev2.id);
        assertEquals(1, it.vote.size());
        assertEquals(1, io.vote.size());
        assertEquals(1, ie.vote.size());
        assertEquals(1, it.noOfVotes);
        assertEquals(1, io.noOfVotes);
        assertEquals(1, ie.noOfVotes);
        assertEquals(3, dev2.voteIdea.size());
        it.unvote(dev2.id);
        assertEquals(2, dev2.voteIdea.size());
        io.unvote(dev2.id);
        assertEquals(1, dev2.voteIdea.size());
        ie.unvote(dev2.id);
         assertEquals(0,it.noOfVotes);
        assertEquals(0,io.noOfVotes);
        assertEquals(0,ie.noOfVotes);
        assertEquals(0, it.vote.size());
        assertEquals(0, io.vote.size());
        assertEquals(0, ie.vote.size());
        assertEquals(0, dev2.voteIdea.size());
    }

    @Test
    public void inviteesIntoAHiddenTopic() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        IdeaDeveloper dev2 = new IdeaDeveloper();
        dev2.save();
        IdeaDeveloper dev3 = new IdeaDeveloper();
        dev3.save();
        IdeaDeveloper dev4 = new IdeaDeveloper();
        dev4.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Org");
        Entities ent = Entities.create(org);
        Topic t = Topic.create(dev, ent);
        assertNotNull(t.topicEnt);
        assertNull(t.topicOrg);
        org.members.add(dev2);
        org.members.add(dev3);
        org.members.add(dev4);
        assertEquals(3, org.members.size());
        assertEquals(3, t.invitees().size());
        assertEquals(0,t.joined.size());
        t.inviteToHidden(dev2.id);
        assertEquals(1,t.joined.size());
        t.inviteToHidden(dev3.id);
        assertEquals(2,t.joined.size());
        t.inviteToHidden(dev4.id);
        assertEquals(3,t.joined.size());

    }

    @Test
    public void TopicRequestCreation() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Org");
        assertEquals(0, Request.count());
        Request r = Request.RequestMe(org, dev, "TopicRequestTitle", "TopicRequestBody");
        assertEquals(1, Request.count());
        assertEquals(1, org.requests.size());
        assertEquals(1, dev.requests.size());
        r.delete2();
        assertEquals(0, Request.count());
        assertEquals(0, org.requests.size());
        assertEquals(0, dev.requests.size());
    }

    @Test
    public void DraftTopicSave() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        Organization org = Organization.create(lead, "Org");
        assertEquals(0, Request.count());
        Request r = Request.draftMeOrg("TopicRequestTitle", "TopicRequestBody", dev, org.id);
        assertEquals(1, Request.count("type=?", "TopicDraft"));
        assertEquals(0, org.requests.size());
        assertEquals(1, dev.requests.size());
        r.delete2();
        assertEquals(0, Request.count());
        assertEquals(0, org.requests.size());
        assertEquals(0, dev.requests.size());
    }
}
